package cn.snews.Servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.News;
import news.topic;
import toolDAO.Page;
import dao.NewsDAO;
import dao.TopicDAO;

public class IndexServlet extends HttpServlet {

	private static final long serialVersionUID = -8970792174698745776L;

	public IndexServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		List<News> news = null;
		request.getSession().setAttribute("hah", "values");
		Map<topic, List<News>> map = new HashMap<topic, List<News>>();

		List<topic> t = TopicDAO.showIndex();
		for (topic top : t) {
			news = NewsDAO.find(0, 6, (top.getTid()));
			map.put(top, news);
		}

		List<topic> tlist = TopicDAO.showAll();

		Integer tid = null;
		try {
			tid = Integer.parseInt(request.getParameter("tid"));
		} catch (Exception e) {
		}// 忽略所有异常
		int pageNo = 1;
		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception e) {
		}// 忽略所有异常
		Page<News> p = NewsDAO.DAO.find1(pageNo, tid);

		request.setAttribute("map", map);// 返回置顶的主题的新闻
		request.setAttribute("tlist", tlist);// 返回所有的主题
		request.setAttribute("p", p);// 主页显示的新闻
		request.setAttribute("tid", tid);
		request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,
				response);
	}

	public void init() throws ServletException {
	}

}
